//
//  WeatherViewController.m
//  QuLuoMianApp
//
//  Created by zyz on 2017/12/24.
//  Copyright © 2017年 科大讯飞. All rights reserved.
//

#import "WeatherViewController.h"


@interface WeatherViewController ()

@property (nonatomic,strong) NSDictionary * dataDict;


@property (weak, nonatomic) IBOutlet UILabel *currentWeatherL;

@property (weak, nonatomic) IBOutlet UILabel *detailInfoL;

@property (weak, nonatomic) IBOutlet UILabel *updateTimeL;

@property (weak, nonatomic) IBOutlet UIImageView *weatherIcon;

@end

@implementation WeatherViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.title = @"罗免天气";
    
    [self loadNetData];
}

- (void)loadNetData{
    
    NSString *urlStr = @"http://weather.51wnl.com/weatherinfo/GetMoreWeather?cityCode=101290109&weatherType=1";
    NSURL *url = [NSURL URLWithString: urlStr];
    NSMutableURLRequest *request = [[NSMutableURLRequest alloc]initWithURL: url cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval: 10];
    [request setHTTPMethod: @"GET"];



    NSURLSession *session = [NSURLSession sharedSession];
    NSURLSessionDataTask *task = [session dataTaskWithRequest:request completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
        if (error) {
            NSLog(@"Httperror: %@%ld", error.localizedDescription, error.code);
        } else {
            NSInteger responseCode = [(NSHTTPURLResponse *)response statusCode];
                        NSString *responseString = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
            NSLog(@"HttpResponseCode:%ld", responseCode);
                                    NSLog(@"HttpResponseBody %@",responseString);
            NSDictionary *tmpDataDic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableLeaves error:nil];
            if (tmpDataDic.count != 0) {
                self.dataDict = tmpDataDic[@"weatherinfo"];
                
                dispatch_async(dispatch_get_main_queue(), ^{
                     [self configUI];
                });
                
            }else{
                NSLog(@"数据无法请求");
            }
        }
    }];
    [task resume];
}
   
- (void)configUI{
    
    self.currentWeatherL.text = [NSString stringWithFormat:@"%@",self.dataDict[@"temp"]];
    
    self.updateTimeL.text = [NSString stringWithFormat:@"%@更新",self.dataDict[@"time"]];
    
    self.detailInfoL.text = [NSString stringWithFormat:@"湿度：%@   %@%@",self.dataDict[@"SD"],self.dataDict[@"WD"],self.dataDict[@"WS"]];
}

@end
